//
//  GQJContainerController.m
//  TotalDemo
//
//  Created by gujian on 16/7/19.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJContainerController.h"

@interface GQJContainerController()
{
    UIViewController *_presentViewController;
    CGSize _size;
    UIPanGestureRecognizer *_panGestureRecognizer;
}

@end

@implementation GQJContainerController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    
}

- (void)addGesture
{
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesutre:)];
    _panGestureRecognizer.minimumNumberOfTouches = 1;
    _panGestureRecognizer.maximumNumberOfTouches = 1;
    [_presentViewController.view addGestureRecognizer:_panGestureRecognizer];
    
}

- (void)removeGesture
{
    [_panGestureRecognizer removeTarget:self action:NULL];
    _panGestureRecognizer = nil;
}

- (void)handlePanGesutre:(UIPanGestureRecognizer *)pangesture
{
    if (_panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        
    }
    
    CGPoint translation = [_panGestureRecognizer translationInView:self.view];
    
    CGPoint center = _presentViewController.view.center;
    center.x += translation.x;
    center.y += translation.y;
    
    CGFloat centerHeightOffset = CGRectGetHeight(_presentViewController.view.frame) / 2.0;
    CGFloat centerWidthOffSet = CGRectGetWidth(_presentViewController.view.frame) / 2.0;

    //左边，上边最小值
    if (center.x - centerWidthOffSet < 0 ) {
        center.x = centerWidthOffSet;
    }
    
    if (center.y - centerHeightOffset < 0) {
        center.y = centerHeightOffset;
    }
    
    
    CGFloat maxinumY = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(_presentViewController.view.frame);
    if (center.y - centerHeightOffset > maxinumY) {
        center.y = maxinumY + centerHeightOffset;
    }
    
    CGFloat maxinumX = CGRectGetWidth(self.view.bounds) - CGRectGetWidth(_presentViewController.view.frame);
    if (center.x - centerWidthOffSet > maxinumX) {
        center.x = maxinumX + centerWidthOffSet;
    }
    
    _presentViewController.view.center = center;
    
    [_panGestureRecognizer setTranslation:CGPointZero inView:self.view];
    
    
}

#pragma mark - Public methods
- (void)presentViewController:(UIViewController *)presentViewController size:(CGSize)size
{
    if (_presentViewController) {
        [self dismissCurrentViewController];
    }
    
    _presentViewController = presentViewController;
    _size = size;
    CGSize adjustedSize = CGSizeMake(MIN(_size.width, CGRectGetWidth(self.view.bounds)),
                                     MIN(_size.height, CGRectGetHeight(self.view.bounds)));
    
    //将presentController置于status bar 之下，水平居中
    CGFloat heightOffset = 20;
    CGFloat widthOffset = (CGRectGetWidth(self.view.bounds) - adjustedSize.width) / 2.0;
    
    CGRect presentViewFrame = CGRectMake(widthOffset, heightOffset, adjustedSize.width, adjustedSize.height);
    
    [self addChildViewController:_presentViewController];
    _presentViewController.view.frame = presentViewFrame;
    [self.view addSubview:_presentViewController.view];
    
    [self addGesture];
    
    [_presentViewController didMoveToParentViewController:self];
    
}

- (void)dismissCurrentViewController
{
    if (!_presentViewController) {
        return;
    }
    
    [_presentViewController willMoveToParentViewController:nil];
    
    [self removeGesture];
    
    [_presentViewController.view removeFromSuperview];
    [_presentViewController removeFromParentViewController];
    
}


@end























